package com.stu.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 环绕通知
 * @author zy
 * @date 2025/3/31
 */
@Component
@Aspect
public class LogAroundAspect {
    @Pointcut("execution(* com.stu.dao.*.*(..))")
    public void pointcut() {}

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = null;
        try{
            String methodName = joinPoint.getSignature().getName();
            System.out.println("before前置通知:"+methodName);

            proceed = joinPoint.proceed();  // 目标方法调用
            //后置返回通知一旦目标方法调用的时候出现了异常,那么不会执行.
            System.out.println("后置返回通知(返回结果):"+proceed);
        }catch (Throwable e){
            System.out.println("异常通知:"+e.getMessage());
            e.printStackTrace();
        }finally {
            System.out.println("后置通知");//后置通知无论是否出现异常都会执行
        }
        return proceed;
    }
}
